Creating Compute Engine VMs

Compute VM#

In this lesson, we will explore each property of the Compute Instance. We will use Console(GUI) for this. Later on, once we cover all the properties, we will see how to create an equivalent Google SDK command with a single click.

Introduction#

Just to recap, virtual machines (Compute Engine) is useful when you need ultra-high performance using Persistent SSDs, Local SSDs, GPUs, and high memory and compute power. Also, when container orchestration is not required.

VMs are an essential part of many infrastructures. Even Virtual Private Clouds are also created using Compute Engine.

Workloads such as 3D rendering which requires multiple GPUs or batch processing in a cost-efficient manner using Preemptive instances are great examples of VM use cases. So, let’s create our first virtual machine.

Creating a virtual machine using GUI#

Open Main menu > Compute Engine > VM Instances.

If the compute engine API is not enabled you might see a text saying API is being enabled. Once API is enabled.

You should see the dashboard to create a virtual machine. Click on the create instance button.

Click on "CREATE INSTANCE" button.
Click on "CREATE INSTANCE" button.

This will open a form containing all the properties of the Compute Engine. Let’s understand each input field one by one.

In the left side pane, you will see the multiple ways to create a VM. In this lesson, we will focus on the first one, creating a “New VM instance”. So, let’s fill the form.

  • Name: This is the user-defined name unique for this project.

  • Label: This is a key pair based values which help is filtering and searching resource. For example, you can add a label for this instance as a purpose: demo.

  • Region and zone: This is the geographical location of the instance. The more it is close to users less will be the latency.

  • Machine Configuration: You can choose a machine type specific to your need. Keep an eye on the price shown on the right side of the form.

If you expand the CPU platform and GPU section you can select the CPU family and GPU. GPUs are region/zone specific. These are available for specific zones only.

A form to create instance will open up.
A form to create instance will open up.

Leave the checkboxes as it is for now. If you hover over the question mark present above the field labels, it will display the purpose of the fields. For the boot disk, we have 4 options. Click on the “Change” button to see all of those.

The available options to create an instance are as follows:

  1. From a standard Public image.

  2. From a Custom image from any of our projects.

  3. From a Snapshots.

  4. From Existing bootable disks of other instances.

For now, we will go with the standard Debian preselected option. So exit the form by clicking the Cancel button.

  • Identity and API access: You can select any existing service account for this VM or go with the default one. In the access section, you can define what all APIs this VM can access.

  • Firewall: This is a shortcut for allowing HTTP and HTTPS traffic. The firewall can be configured using tags as well.

Hover over question mark to get the information about the field.
Hover over question mark to get the information about the field.

Expand the management and security section by clicking on the text. There are multiple things but for now, look at the startup script and Preemptibility option.

  • Startup script: For any software installation or OS level commands we can use this section. Commands entered in this section are executed after the instance is booted.

  • Preemptibility: To make the instance preemptible select from the dropdown. Notice the drastic change in price when you make an instance preemptible. Preemptive instances are 80% cheaper than regular ones. For now, keep it a regular instance and keep it unchecked.

Once all the form is filled look at the bottom of the form below the create button. You will see the equivalent CLI command and REST request to create the instance as well.

Startup script section.
Startup script section.
Click on the "Create" button.
Click on the "Create" button.

Once done hit the create button and notice how fast a Linux instance boots up. You won’t be able to connect to this instance as of now.

In the next lesson, we will see how to resolve the issue and also how to create and connect to the instance.

Creating instance using gcloud#

There is another way to create an instance using gcloud SDK. Use the following command to create a new instance using gcloud SDK.

gcloud beta compute --project=[PROJECT_NAME] instances create instance-2 --zone=us-central1-a

This command will create a new instance with all the default values for the other attributes. Type gcloud beta compute instances create --help to know all the available parameters while creating the virtual machine.

 gcloud beta compute --project=gcp-headstart-educative-308308 instances create instance-2 --zone=us-central1-a
Created [https://www.googleapis.com/compute/beta/projects/gcp-headstart-educative-308308/zones/us-central1-a/instances/instance-2].
NAME        ZONE           MACHINE_TYPE   PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP     STATUS
instance-2  us-central1-a  n1-standard-1               10.128.0.3   35.192.159.107  RUNNING

Now, there will be two virtual machines running in our project. Namely instance-1 and instance-2. You can list all the available VMs using gcloud beta compute instances list command.

 gcloud beta compute instances list
NAME        ZONE           MACHINE_TYPE   PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP     STATUS
instance-1  us-central1-a  e2-medium                   10.128.0.2   34.122.80.150   RUNNING
instance-2  us-central1-a  n1-standard-1               10.128.0.3   35.192.159.107  RUNNING

Try out all the above commands in the terminal given below or in the Cloud shell. You need to login to access your GCP platform CLI. Click on the terminal and the command login will start instantly.

Terminal 1
Terminal

Click to Connect...

Make sure to delete instances after the demo to avoid any additional charges. You can delete the instances using two methods.

Using gcloud SDK

Use the command gcloud beta compute instances delete [INSTANCE NAMES] --zone [ZONE].

This will delete the provided instances of the specified zones.

Using GUI

  • Open the VM instance dashboard using the Main menu> Compute Engine > VM Instance.

  • Select the instances you want to delete.

  • Click on the more actions button (3 ellipses) and then click the delete button to delete the instances.

We will delete the instance using the command because that’s an easier method.

 gcloud beta compute instances delete instance-1 instance-2 --zone us-central1-a
The following instances will be deleted. Any attached disks configured
 to be auto-deleted will be deleted unless they are attached to any
other instances or the `--keep-disks` flag is given and specifies them
 for keeping. Deleting a disk is irreversible and any data on the disk
 will be lost.
 - [instance-1] in [us-central1-a]
 - [instance-2] in [us-central1-a]

Do you want to continue (Y/n)?  y

Deleted [https://www.googleapis.com/compute/beta/projects/gcp-headstart-educative-308308/zones/us-central1-a/instances/instance-1].
Deleted [https://www.googleapis.com/compute/beta/projects/gcp-headstart-educative-308308/zones/us-central1-a/instances/instance-2].

Lab#

Complete the following lab to strengthen your understanding of Compute Engine. Lab uses the new service “load balancer” which we will cover in the network section. This will be a good warm-up for networking.

In the next lesson, we will see the advanced use of Compute Engine by creating templates and groups.

Preemptive VMs and Custom VMs

Connecting to VM Instance